package com.eastfair.security.component;

import cn.hutool.core.util.ClassUtil;
import com.alibaba.fastjson.JSON;
import com.eastfair.core.context.ContextConstants;
import com.eastfair.core.context.StaticContextUtil;
import com.eastfair.core.dynamic.DynamicAnno;
import com.eastfair.core.dynamic.FieldTypeUtil;
import com.eastfair.dynamic.DynamicPackage;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.security.formfield.ConvertByFormField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;

/**
 * springboot容器启动完成后运行
 */
@Component
@Slf4j
public class ApplicationRunnerCore implements ApplicationRunner {
    @Autowired
    ProjectCoreServiceFeign projectCoreServiceFeign;

    @Autowired
    ConvertByFormField convertByFormField;

    //暂时写死
    private String voPackScan = "com.eastfair";
    private static final String LOCAL = "local";
    private static final String DEV = "dev";
    private static final String TEST = "test";
    private static final String PROD = "prod";

    @Value("${spring.profiles.active}")
    private String profilesActive;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("ApplicationRunnerCore is execute");
        //VO包扫描
        voScanByAllClass();
        //记录IP地址
        try {
            String ip = InetAddress.getLocalHost().getHostAddress();
            StaticContextUtil.setIpAddr(ip);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        //记录是否是开发环境
        if (DEV.equalsIgnoreCase(profilesActive)) {
            StaticContextUtil.setProfilesActive(DEV);
        } else if (TEST.equalsIgnoreCase(profilesActive)) {
            StaticContextUtil.setProfilesActive(TEST);
        } else if (LOCAL.equalsIgnoreCase(profilesActive)) {
            StaticContextUtil.setProfilesActive(LOCAL);
        } else {
            StaticContextUtil.setProfilesActive(PROD);
        }
        //执行业务代码
        executeBusinessRunner();


    }

    /**
     * VO包扫描
     */
    public void voScanByAllClass() {
        Set<Class<?>> classSet = new HashSet<>();
        for (String packageName : DynamicPackage.packAgeList) {
            classSet.addAll(ClassUtil.scanPackage(packageName));
        }
        if (classSet != null && classSet.size() > 0) {
            for (Class class2 : classSet) {
                //没有DTO标识也能加载
                if (DynamicAnno.isVoInfoClass(class2)) {
                    FieldTypeUtil.addVoBusinClass(class2);
                }
            }
        }
        log.info("加载bean 类 {} " + JSON.toJSONString(FieldTypeUtil.getBuinClass()));
    }


    /**
     * 执行业务代码
     */
    public void executeBusinessRunner() {
        System.out.println("executeBusiness is execute");
        //每次重启服务创建一次workid
        Long l = projectCoreServiceFeign.createWorkNode();
        if (l != null && l != 0) {
            ContextConstants.WORK_ID = l;
        }



    }

}
